Central Article

Creating a Two-Way Video Chat Application in Macromedia Central


Table of Contents

  1. Introduction
  2. The vChat Application
  3. The Client Side: Building the Application
  4. The Client Side: Building the Pod
  5. Lessons Learned

The vChat Application

In this tutorial I show you how to create an application that allows multiple people to log on to a "room" and see who's online. They can click on a user's name and engage in a one-on-one audio/video conversation with them. The whole thing happens within a pod; the application functions as a help page (see Figure 1).

The vChat application shows users a quick overview of the different forms that compose it (login, user list, and video chat)

Figure 1. The vChat application shows users a quick overview of the different forms that compose it (login, user list, and video chat)

The Server-Side Code

When I build a Flash Communication Server application, I always start on the server side. It's much easier and it helps you think about how your application is going to work. Since vChat is such a simple application, a simple users shared object suffices. All you need to do is assign a unique ID to each user who connects, use that ID as the name of the slot in the shared object, and keep a simple object in the slot. The object contains the user's chosen username and a talkingTo pointer, which is null if the user is not talking to anyone but contains the ID of the user they're talking to when they're having a video chat.

Here's all the server-side code you need:

application.onAppStart = function()
{ 
   //Get a hold of the users Shared Object
   this.users_so = SharedObject.get("users",false);
}

application.onConnect = function(client, name)
{
   //find next available ID
   var i = 0;
   var tempObj = this.users_so.getProperty("u"+i);
   while (tempObj != null) {
      i++;
      tempObj = this.users_so.getProperty("u"+i);
   }
   //Save the ID in the client object
   client.id = "u"+i;

   //Add user to SO (this will trigger an onSync on the clients)
   this.users_so.setProperty(client.id, {name: name, talkingTo:null});

   //Accept this user's connection
   application.acceptConnection(client);

   //Tell this client what their ID is
   client.call("setID", null, client.id);
}

application.onDisconnect = function(client)
{
   //Get a lock because we might do 2 setProperty calls 
   //and we want only onSync to get triggered
   this.users_so.lock();

      //If the user is talking to someone, clean the other user's talkingTo field.
      var obj = this.users_so.getProperty(client.id);
      if (obj.talkingTo != null) {
         var otherUser = this.users_so.getProperty(obj.talkingTo);
         otherUser.talkingTo = null;
         this.users_so.setProperty(obj.talkingTo, otherUser);
      }

      //Clean the slot for the client that just left.
      this.users_so.setProperty(client.id, null);

   //Now the onSync will get triggered on the remaining clients
   this.users_so.unlock();
}

Save this code in a file called main.asc in a folder called central_vchat (I like to keep my application folder names all lowercase) under your Flash Communication Server applications folder.